/********************************************************************************
* Description: tcp interface wrapper, it is only one of server or client
* Author: hsqiang
* Create Date: 2023-06-21
********************************************************************************/
#ifndef TCP_SERVICE_HPP_
#define TCP_SERVICE_HPP_

#include "net_service.hpp"
#include "acceptor.hpp"
#include "connector.hpp"
#include "connection.hpp"
#include "channel.hpp"
#include "event_loop_pool.hpp"

namespace event_framework
{
    class TcpService : public NetService
    {
    public:
        explicit TcpService(EventLoop& loop, const EndPoint& ep)
            : NetService(loop, ep) {}
        ~TcpService() = default;
        virtual bool Init() override;
        virtual bool Fini() override;
        void SetNewConnCallback(NewConnCallback&& cb)
        {
            ASSERT(cb);
            newConnCallback = std::move(cb);
        }
        // allow up to <backlog> half-connection in kernel queue
        bool Listen(int backlog = 64);
        bool Connect(const EndPoint& peer);
        
        void CloseConnection(int socket)
        {
            connMgr.CloseConnection(socket);
        }
        void CloseAllConnection()
        {
            connMgr.CloseAll();
        }
        void UseNewLoopForNewConn(EventLoopPool* p) { ASSERT(p != nullptr); pool = p; }
    private:
        // socket is peer
        void NewConnection(Socket& socket);

        AcceptorPtr acceptor;
        ConnectorPtr connector;
        ConnectionMgr connMgr;
        NewConnCallback newConnCallback;
        EventLoopPool* pool;
    };
}

#endif